package cn.psvmc.cxzapi.controller.goods;

import cn.psvmc.cxzapi.model.common.DelEntity;
import cn.psvmc.cxzapi.model.common.DetailEntity;
import cn.psvmc.cxzapi.model.common.GoodsEntity;
import cn.psvmc.cxzapi.model.common.MarketEntity;
import cn.psvmc.cxzapi.model.goods.TGoodsExample;
import cn.psvmc.cxzapi.response.PageResult2;
import cn.psvmc.cxzapi.response.Result;
import cn.psvmc.cxzapi.response.ResultGenerator;
import cn.psvmc.cxzapi.service.goods.TGoodsExampleService;
import cn.psvmc.cxzapi.util.PageUtil;
import cn.psvmc.cxzapi.util.ToolUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 商品推荐实例管理
 * @Author auto
 * @Date 2024-08-13
 */
@RestController
@RequestMapping("/example")
public class TGoodsExampleController {

    @Resource
    private TGoodsExampleService tGoodsExampleService;

    /**
    * @Author auto 2024-08-13 新增商品推荐实例表
    * @Param tGoodsExample 商品推荐实例表
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/add")
    public Result add(@RequestBody TGoodsExample tGoodsExample) {
        ToolUtils.validation(new Object[]{
                tGoodsExample.getTagNames(), "id", 2
                , tGoodsExample.getName(), "分类名称", 2
                , tGoodsExample.getInfo(), "分类tip", 2
        });
        int addResult = tGoodsExampleService.add(tGoodsExample);
        if (addResult > 0) {
            return ResultGenerator.genSuccessResult("保存成功！");
        }
        return ResultGenerator.genFailResult("保存失败！");
    }

    /**
    * @Author auto 2024-08-13 更新商品推荐实例表
    * @Param tGoodsExample 商品推荐实例表模型
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/edit")
    public Result edit(@RequestBody TGoodsExample tGoodsExample) {
        ToolUtils.validation(new Object[]{tGoodsExample.getGeid(), "id", 1, tGoodsExample.getRversion(), "版本号", 1});
        int updateResult = tGoodsExampleService.edit(tGoodsExample);
        if (updateResult > 0) {
            return ResultGenerator.genSuccessResult("更新成功！");
        }
        return ResultGenerator.genFailResult("更新失败！");
    }

    /**
    * @Author auto 2024-08-13 查询商品推荐实例表详情
    * @Param tGoodsExample
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/detail")
    public Result detail(@RequestBody DetailEntity entity) {
        Integer id = entity.getId();
        ToolUtils.validation(new Object[]{id, "id", 1});
        return ResultGenerator.genSuccessResult("查询成功", tGoodsExampleService.detail(id));
    }

    /**
    * @Author auto 2024-08-13 删除商品推荐实例表
    * @Param tGoodsExample
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/delete")
    public Result delete(@RequestBody DelEntity entity) {
        Integer id = entity.getId();
        Integer rversion = entity.getRversion();
        ToolUtils.validation(new Object[]{id, "id", 1, rversion, "版本号", 1});
        int delResult = tGoodsExampleService.delete(id, rversion);
        if (delResult > 0) {
            return ResultGenerator.genSuccessResult("删除成功！");
        }
        return ResultGenerator.genFailResult("删除失败！");
    }

    /**
    * @Author auto 2024-08-13 商品列表
    * @Param entity 分页查询通用实体类
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/list_goods")
    public PageResult2 listGoods(@RequestBody GoodsEntity entity) {
        Page<TGoodsExample> queryPage = new Page<>(entity.getPage(), entity.getSize());
        return PageUtil.getPageResult2(tGoodsExampleService.listGoods(queryPage, entity));
    }

    /**
     * @Description 列表
     * @Date 2024/8/18 16:16
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list")
    public Result list(@RequestBody MarketEntity entity) {
        return ResultGenerator.genSuccessResult("查询成功！", tGoodsExampleService.listAll(entity));
    }
}
